﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ObjectPersistence;
using KYO.DAL;

public partial class Admin_Controls_Mod_Tab_TabEdit : Controller<Tab>
{
    TabAssembler service = new TabAssembler();
    List<Message> list = new List<Message>();
    ViewControlBase<Tab> ViewControlBase1;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            MessageBox1.Visible = false;
            object o = Request.QueryString["id"];
            if (o != null)
            {
                int id = int.Parse(o.ToString());
                Model = service.ReadSingleByKey(id);
                if (Model == null) View("Warring");
                ViewControlBase1.UpdateView(Model);
            }
            else
                Model = new Tab();
        }
    }

    protected override void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        SaveTool1.SaveClick += new EventHandler(SaveTool1_SaveClick);
        SaveTool1.CancelClick += new EventHandler(SaveTool1_CancelClick);
        SaveTool1.ApplyClick += new EventHandler(SaveTool1_ApplyClick);
        ViewControlBase1 = (ViewControlBase<Tab>)FindControl("Tab1");
    }

    void SaveTool1_ApplyClick(object sender, EventArgs e)
    {

        try
        {
            Model = ViewControlBase1.UpdateModel(Model);
            service.MakePersistent(Model, true);
            list.Add(new Message("Insert Is successful!"));
            ViewMessage(list, MessageType.Success);

        }
        catch (Exception ex)
        {
            list.Add(new Message("Update Is Fail!"));
            list.Add(new Message(ex.Message));
            ViewMessage(list, MessageType.Error);
        }
    }

    void SaveTool1_CancelClick(object sender, EventArgs e)
    {
        View("Tab", "List");
    }

    void SaveTool1_SaveClick(object sender, EventArgs e)
    {


        try
        {
            ViewControlBase1.UpdateModel(Model);
            service.MakePersistent(Model, true);
            View("Tab", "List");
        }
        catch (Exception ex)
        {


            list.Add(new Message("Update Is fail!"));
            list.Add(new Message(ex.Message));
            ViewMessage(list, MessageType.Error);
        }

    }




}
